package com.bw.posts.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.posts.domain.SysDept;
import com.bw.posts.service.SysDeptService;
import com.bw.posts.units.Result;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/posts")
public class PostController {
    @Autowired
    private SysDeptService sysDeptService;

    @RequestMapping("/higHlight")
    public Result higHlight(String name){

        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();

        if(name!=null && name!=""){
            wrapper.like("name",name);
        }

        List<SysDept> list = sysDeptService.list(wrapper);

        list.forEach(SysDept->{
            String s = SysDept.getName().replaceAll(name, "<font color='red'>" + name + "</font>");
            SysDept.setName(s);
        });


        return Result.success(list);
    }

    /**
     * 查询部门id
     * @return
     */
    @RequestMapping("/getdeptId")
    public R getdeptId(String deptname){
        LambdaQueryWrapper<SysDept> sysDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDeptLambdaQueryWrapper.eq(SysDept::getName,deptname);
        SysDept one = sysDeptService.getOne(sysDeptLambdaQueryWrapper);
        return R.ok(one.getId());
    }
    @RequestMapping("list")
    public Result list(){
        List<SysDept> deptList = sysDeptService.list();
        return Result.success(deptList);
    }

    @RequestMapping("/updateDept")
    public Result updateDept(@RequestBody SysDept dept){
        boolean b = sysDeptService.updateById(dept);
        if (!b) {
            return Result.fail("修改失败");
        }
        return Result.success();
    }

    @RequestMapping("/deleteDept")
    public Result deleteDept(Integer id){
        boolean b = sysDeptService.removeById(id);
        if (!b) {
            return Result.fail("删除失败");
        }
        return Result.success();
    }

    @RequestMapping("/saveDept")
    public Result saveDept(@RequestBody SysDept dept){
        boolean save = sysDeptService.save(dept);
        if (!save) {
            return Result.fail("添加失败");
        }
        return Result.success();
    }

    @RequestMapping("/deptlist")
    public Result deptlist(String name){
        List<SysDept> deptList = sysDeptService.list();
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        if(name!=null && name!=""){
           wrapper.like("name",name);
            List<SysDept> list = sysDeptService.list(wrapper);
            list.forEach(SysDept->{
                String s = SysDept.getName().replaceAll(name, "<font color='red'>" + name + "</font>");
                SysDept.setName(s);
            });
            return Result.success(list);
        }
        //转为树形结构
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setIdKey("id");
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysDept, String>build(deptList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName()+"");
                    tree.putExtra("createDate", treeNode.getCreateDate());
                    tree.putExtra("status", treeNode.getStatus());
                });
        return Result.success(treeNodes);
    }

    @RequestMapping("/parentdept")
    public R parentdept(){
        LambdaQueryWrapper<SysDept> sysDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDeptLambdaQueryWrapper.in(SysDept::getParentId,0,1);
        List<SysDept> list = sysDeptService.list(sysDeptLambdaQueryWrapper);
        return R.ok(list);
    }

    @RequestMapping("/getdeptbypid")
    public R getdeptbypid(Integer parentId){
        LambdaQueryWrapper<SysDept> sysDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDeptLambdaQueryWrapper.eq(SysDept::getId,parentId);
        SysDept one = sysDeptService.getOne(sysDeptLambdaQueryWrapper);
        return R.ok(one.getId());
    }

    @RequestMapping("/getparentdeptsupdate")
    public R getparentdeptsupdate(Integer id){
        LambdaQueryWrapper<SysDept> sysDeptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDeptLambdaQueryWrapper.in(SysDept::getParentId,0,1)
                .ne(SysDept::getId,id);
        List<SysDept> list = sysDeptService.list(sysDeptLambdaQueryWrapper);
        System.out.println("------------"+list);
        return R.ok(list);
    }
}
